## R Script Output -------------------------------------------------------------
# Appendix Table C.1: Descriptive Statistics for County-Level Analysis, 2012 vs. 2016
# Appendix Table C.2: Descriptive Statistics for County-Level Analysis, 2012 vs. 2020 
# Appendix Table C.3: Descriptive Statistics for County-Level Analysis, 2008 vs. 2012


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()


# load packages
pkg <- c("tidyverse",
         "stargazer")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "data-merge-county.RData", sep = ""))

# set df
df <- county.df

df.treat.1 <- df %>% 
  filter(year == 2012 | year == 2016) 

df.treat.2 <- df %>% 
  filter(year == 2012 | year == 2020) 

df.placebo <- df %>% 
  filter(year == 2008 | year == 2012) 


## set var and var name correspondence -----------------------------------------
var.df <- tibble(
  var = c("dem_share", 
          "anticorrupt",
          "placebo_anticorrupt_2012",
          "china_undergraduate_county_sqmi_2012",
          "china_graduate_county_sqmi_2012",
          "india_undergraduate_county_sqmi_2012",
          "cn_ug_sqmi_cat_2012_bi_high",
          
          "anticorrupt:cn_ug_sqmi_cat_2012_bi_high",
          "anticorrupt:cn_ug_sqmi_cat_2012_tri_medium",
          "anticorrupt:cn_ug_sqmi_cat_2012_tri_high",
          
          "anticorrupt:cn_g_sqmi_cat_2012_biCN-G-high",
          "anticorrupt:cn_g_sqmi_cat_2012_triCN-G-medium",
          "anticorrupt:cn_g_sqmi_cat_2012_triCN-G-high",
          
          "anticorrupt:ind_ug_sqmi_cat_2012_biIND-UG-high",
          "anticorrupt:ind_ug_sqmi_cat_2012_triIND-UG-medium",
          "anticorrupt:ind_ug_sqmi_cat_2012_triIND-UG-high",
          
          "placebo_anticorrupt_2012:cn_ug_sqmi_cat_2012_bi_high",
          "placebo_anticorrupt_2012:cn_ug_sqmi_cat_2012_tri_medium",
          "placebo_anticorrupt_2012:cn_ug_sqmi_cat_2012_tri_high",
          
          "placebo_anticorrupt_2012:cn_g_sqmi_cat_2012_biCN-G-high",
          "placebo_anticorrupt_2012:cn_g_sqmi_cat_2012_triCN-G-medium",
          "placebo_anticorrupt_2012:cn_g_sqmi_cat_2012_triCN-G-high",
          
          "placebo_anticorrupt_2012:ind_ug_sqmi_cat_2012_biIND-UG-high",
          "placebo_anticorrupt_2012:ind_ug_sqmi_cat_2012_triIND-UG-medium",
          "placebo_anticorrupt_2012:ind_ug_sqmi_cat_2012_triIND-UG-high",
          
          # controls
          "share_pop_county_female",
          "share_pop_5_17_county",
          "share_pop_18_24_county",
          "share_pop_25_34_county",
          "share_pop_35_44_county",
          "share_pop_45_54_county",
          "share_pop_55_64_county",
          "share_pop_65_county",
          
          "share_pop_white_county",
          "share_pop_black_county",
          "share_pop_hispanic_county",
          "share_pop_asian_county",
          "share_pop_cn_county",
          
          "share_foreign_born_county",
          "share_edu_ba_above_county",
          "share_enroll_county",
          "ln_pop_density_county",
          "effective_tax_county", 
          "ipw_county",
          "employ_rate_county", 
          "median_household_income_county",
          "vacancy_county",
          
          # trending effects of controls
          "anticorrupt:share_pop_county_female",
          "anticorrupt:share_pop_5_17_county",
          "anticorrupt:share_pop_18_24_county",
          "anticorrupt:share_pop_25_34_county",
          "anticorrupt:share_pop_35_44_county",
          "anticorrupt:share_pop_45_54_county",
          "anticorrupt:share_pop_55_64_county",
          "anticorrupt:share_pop_65_county",
          
          "anticorrupt:share_pop_white_county",
          "anticorrupt:share_pop_black_county",
          "anticorrupt:share_pop_hispanic_county",
          "anticorrupt:share_pop_asian_county",
          "anticorrupt:share_pop_cn_county",
          
          "anticorrupt:share_foreign_born_county",
          "anticorrupt:share_edu_ba_above_county",
          "anticorrupt:share_enroll_county",
          "anticorrupt:ln_pop_density_county",
          "anticorrupt:effective_tax_county", 
          "anticorrupt:ipw_county",
          "anticorrupt:employ_rate_county", 
          "anticorrupt:median_household_income_county",
          "anticorrupt:vacancy_county",
          
          # trending effects of controls in placebo period
          "placebo_anticorrupt_2012:share_pop_county_female",
          "placebo_anticorrupt_2012:share_pop_5_17_county",
          "placebo_anticorrupt_2012:share_pop_18_24_county",
          "placebo_anticorrupt_2012:share_pop_25_34_county",
          "placebo_anticorrupt_2012:share_pop_35_44_county",
          "placebo_anticorrupt_2012:share_pop_45_54_county",
          "placebo_anticorrupt_2012:share_pop_55_64_county",
          "placebo_anticorrupt_2012:share_pop_65_county",
          
          "placebo_anticorrupt_2012:share_pop_white_county",
          "placebo_anticorrupt_2012:share_pop_black_county",
          "placebo_anticorrupt_2012:share_pop_hispanic_county",
          "placebo_anticorrupt_2012:share_pop_asian_county",
          "placebo_anticorrupt_2012:share_pop_cn_county",
          
          "placebo_anticorrupt_2012:share_foreign_born_county",
          "placebo_anticorrupt_2012:share_edu_ba_above_county",
          "placebo_anticorrupt_2012:share_enroll_county",
          "placebo_anticorrupt_2012:ln_pop_density_county",
          "placebo_anticorrupt_2012:effective_tax_county", 
          "placebo_anticorrupt_2012:ipw_county",
          "placebo_anticorrupt_2012:employ_rate_county", 
          "placebo_anticorrupt_2012:median_household_income_county",
          "placebo_anticorrupt_2012:vacancy_county"),
  
  var_name = c(
    "Two-Party Democratic Vote Share",
    "2013 Anti-corruption Campaign (Dummy)",
    "Placebo Anti-corruption (2012)",
    "2012 \\# CHN Int'l Undergraduate Students per Sq. Mi.",
    "2012 \\# CHN Int'l Graduate Students per Sq. Mi.",
    "2012 \\# IND Int'l Undergraduate Students per Sq. Mi.",
    "CN UG in 2012 (Dummy, High)",
    
    "2013 Anti-corruption X CN UG in 2012 (Dummy, High)",
    "2013 Anti-corruption X CN UG in 2012 (Tri., Medium)",
    "2013 Anti-corruption X CN UG in 2012 (Tri., High)",
    
    "2013 Anti-corruption X CN G in 2012 (Dummy, High)",
    "2013 Anti-corruption X CN G in 2012 (Tri., Medium)",
    "2013 Anti-corruption X CN G in 2012 (Tri., High)",
    
    "2013 Anti-corruption X IND UG in 2012 (Dummy, High)",
    "2013 Anti-corruption X IND UG in 2012 (Tri., Medium)",
    "2013 Anti-corruption X IND UG in 2012 (Tri., High)",
    
    "Placebo Anti-corruption (2012) X CN UG in 2012 (Dummy, High)",
    "Placebo Anti-corruption (2012) X CN UG in 2012 (Tri., Medium)",
    "Placebo Anti-corruption (2012) X CN UG in 2012 (Tri., High)",
    
    "Placebo Anti-corruption (2012) X CN G in 2012 (Dummy, High)",
    "Placebo Anti-corruption (2012) X CN G in 2012 (Tri., Medium)",
    "Placebo Anti-corruption (2012) X CN G in 2012 (Tri., High)",
    
    "Placebo Anti-corruption (2012) X IND UG in 2012 (Dummy, High)",
    "Placebo Anti-corruption (2012) X IND UG in 2012 (Tri., Medium)",
    "Placebo Anti-corruption (2012) X IND UG in 2012 (Tri., High)",
    
    # controls
    "Share of Female Pop.",
    "Share of Pop. 5--17",
    "Share of Pop. 18--24",
    "Share of Pop. 25--34",
    "Share of Pop. 35--44",
    "Share of Pop. 45--54",
    "Share of Pop. 55--64",
    "Share of Pop. 65--",
    
    "Share of White Pop.",
    "Share of Black Pop.",
    "Share of Hispanic Pop.",
    "Share of Asian Pop.",
    "Share of CN Pop.",
    
    "Share of Foreign-Born Pop.",
    "Share of Pop. with BA Degree or Above",
    "Share of Pop. Enrolled in College or Above",
    "Population Density (log)",
    "Effective Tax Rate (\\%)",
    "Trade Exposure (IPW)",
    "Employment Rate",
    "Median Household Income (10,000)",
    "Share of Vacant Houses",
    
    # trending effects of controls
    "2013 Anti-corruption X Share of Female Pop.",
    "2013 Anti-corruption X Share of Pop. 5--17",
    "2013 Anti-corruption X Share of Pop. 18--24",
    "2013 Anti-corruption X Share of Pop. 25--34",
    "2013 Anti-corruption X Share of Pop. 35--44",
    "2013 Anti-corruption X Share of Pop. 45--54",
    "2013 Anti-corruption X Share of Pop. 55--64",
    "2013 Anti-corruption X Share of Pop. 65--",
    
    "2013 Anti-corruption X Share of White Pop.",
    "2013 Anti-corruption X Share of Black Pop.",
    "2013 Anti-corruption X Share of Hispanic Pop.",
    "2013 Anti-corruption X Share of Asian Pop.",
    "2013 Anti-corruption X Share of CN Pop.",
    
    "2013 Anti-corruption X Share of Foreign-Born Pop.",
    "2013 Anti-corruption X Share of Pop. with BA Degree or Above",
    "2013 Anti-corruption X Share of Pop. Enrolled in College or Above",
    "2013 Anti-corruption X Population Density (log)",
    "2013 Anti-corruption X Effective Tax Rate (\\%)",
    "2013 Anti-corruption X Trade Exposure (IPW)",
    "2013 Anti-corruption X Employment Rate",
    "2013 Anti-corruption X Median Household Income (10,000)",
    "2013 Anti-corruption X Share of Vacant Houses",
    
    # trending effects of controls in placebo period
    "Placebo Anti-corruption (2012) X Share of Female Pop.",
    "Placebo Anti-corruption (2012) X Share of Pop. 5-17",
    "Placebo Anti-corruption (2012) X Share of Pop. 18-24",
    "Placebo Anti-corruption (2012) X Share of Pop. 25-34",
    "Placebo Anti-corruption (2012) X Share of Pop. 35-44",
    "Placebo Anti-corruption (2012) X Share of Pop. 45-54",
    "Placebo Anti-corruption (2012) X Share of Pop. 55-64",
    "Placebo Anti-corruption (2012) X Share of Pop. 65-",
    
    "Placebo Anti-corruption (2012) X Share of White Pop.",
    "Placebo Anti-corruption (2012) X Share of Black Pop.",
    "Placebo Anti-corruption (2012) X Share of Hispanic Pop.",
    "Placebo Anti-corruption (2012) X Share of Asian Pop.",
    "Placebo Anti-corruption (2012) X Share of CN Pop.",
    
    "Placebo Anti-corruption (2012) X Share of Foreign-Born Pop.",
    "Placebo Anti-corruption (2012) X Share of Pop. with BA Degree or Above",
    "Placebo Anti-corruption (2012) X Share of Pop. Enrolled in College or Above",
    "Placebo Anti-corruption (2012) X Population Density (log)",
    "Placebo Anti-corruption (2012) X Effective Tax Rate (\\%)",
    "Placebo Anti-corruption (2012) X Trade Exposure (IPW)",
    "Placebo Anti-corruption (2012) X Employment Rate",
    "Placebo Anti-corruption (2012) X Median Household Income (10,000)",
    "Placebo Anti-corruption (2012) X Share of Vacant Houses"
  ))


## function to replace variable names ------------------------------------------
replaceVarName <- function(var.vec, var.df){
  # Prepare output vector
  out.vec <- rep(NA, length(var.vec))
  matches <- match(var.vec, var.df$var)
  out.vec <- var.df[matches,]$var_name
  
  if(any(is.na(out.vec))){
    warning(paste("Variable concordence missing: ", 
                  paste(var.vec[is.na(out.vec)], collapse = ", "), 
                  sep = ""))
  } else{
    #print("All variables successfully converted")
  }
  
  return(out.vec)
}


## Table C1 --------------------------------------------------------------------
# select vars
des.table.1 <- df.treat.1 %>%
  select(dem_share,
         anticorrupt,
         
         china_undergraduate_county_sqmi_2012,
         china_graduate_county_sqmi_2012,
         india_undergraduate_county_sqmi_2012,
         
         share_pop_county_female , 
         share_pop_5_17_county , 
         share_pop_18_24_county ,  
         share_pop_25_34_county , 
         share_pop_35_44_county , 
         share_pop_45_54_county, 
         share_pop_55_64_county, 
         share_pop_65_county,  
         
         share_pop_white_county,  
         share_pop_black_county,  
         share_pop_hispanic_county,  
         share_pop_asian_county,  
         share_pop_cn_county,  
         
         share_foreign_born_county,  
         
         share_edu_ba_above_county, 
         share_enroll_county,  
         ln_pop_density_county,  
         
         effective_tax_county,  
         ipw_county,  
         
         employ_rate_county,  
         median_household_income_county,  
         vacancy_county) %>% 
  data.frame()

# set var vector
var.12.16 <- names(des.table.1)

# save
sink(file.path(MAIN_DIR, "Table-C1.txt"))
#sink(file.path(MAIN_DIR, "Table-C1.tex"))
stargazer(des.table.1, 
          type = "text", 
          #type = "latex", 
          label = "tb:des-stats-county-12-16",
          font.size = "footnotesize",
          digits = 2,
          title = "{\\bf Descriptive Statistics for County-Level Analysis, 2012--2016}",
          summary.stat = c("n", "mean", "median", "sd", "min", "max"),
          covariate.labels = replaceVarName(var.vec = var.12.16,
                                            var.df = var.df))
sink()


## Table C2 --------------------------------------------------------------------
# select vars
des.table.2 <- df.treat.2 %>%
  select(dem_share,
         anticorrupt,
         
         china_undergraduate_county_sqmi_2012,
         china_graduate_county_sqmi_2012,
         india_undergraduate_county_sqmi_2012,
         
         share_pop_county_female , 
         share_pop_5_17_county , 
         share_pop_18_24_county ,  
         share_pop_25_34_county , 
         share_pop_35_44_county , 
         share_pop_45_54_county, 
         share_pop_55_64_county, 
         share_pop_65_county,  
         
         share_pop_white_county,  
         share_pop_black_county,  
         share_pop_hispanic_county,  
         share_pop_asian_county,  
         share_pop_cn_county,  
         
         share_foreign_born_county,  
         
         share_edu_ba_above_county, 
         share_enroll_county,  
         ln_pop_density_county,  
         
         effective_tax_county,  
         ipw_county,  
         
         employ_rate_county,  
         median_household_income_county,  
         vacancy_county) %>% 
  data.frame()
    
# set var vector
var.12.20 <- names(des.table.2)

# save
sink(file.path(MAIN_DIR, "Table-C2.txt"))
#sink(file.path(MAIN_DIR, "Table-C2.tex"))
stargazer(des.table.2, 
          type = "text", 
          #type = "latex", 
          label = "tb:des-stats-county-12-20",
          font.size = "footnotesize",
          digits = 2,
          title = "{\\bf Descriptive Statistics for County-Level Analysis, 2012--2020}",
          summary.stat = c("n", "mean", "median", "sd", "min", "max"),
          covariate.labels = replaceVarName(var.vec = var.12.20,
                                            var.df = var.df))
sink()


## Table C3 --------------------------------------------------------------------
# select vars
des.table.placebo <- df.placebo %>%
  select(dem_share,
         anticorrupt,
         
         china_undergraduate_county_sqmi_2012,
         china_graduate_county_sqmi_2012,
         india_undergraduate_county_sqmi_2012,
         
         share_pop_county_female , 
         share_pop_5_17_county , 
         share_pop_18_24_county ,  
         share_pop_25_34_county , 
         share_pop_35_44_county , 
         share_pop_45_54_county, 
         share_pop_55_64_county, 
         share_pop_65_county,  
         
         share_pop_white_county,  
         share_pop_black_county,  
         share_pop_hispanic_county,  
         share_pop_asian_county,  
         share_pop_cn_county,  
         
         share_foreign_born_county,  
         
         share_edu_ba_above_county, 
         share_enroll_county,  
         ln_pop_density_county,  
         
         effective_tax_county,  
         ipw_county,  
         
         employ_rate_county,  
         median_household_income_county,  
         vacancy_county) %>% 
  data.frame()

# set var vector
var.placebo <- names(des.table.placebo)

# save
sink(file.path(MAIN_DIR, "Table-C3.txt"))
#sink(file.path(MAIN_DIR, "Table-C3.tex"))
stargazer(des.table.placebo, 
          type = "text", 
          #type = "latex", 
          label = "tb:des-stats-county-08-12",
          font.size = "footnotesize",
          digits = 2,
          title = "{\\bf Descriptive Statistics for County-Level Analysis, 2008--2012}",
          summary.stat = c("n", "mean", "median", "sd", "min", "max"),
          covariate.labels = replaceVarName(var.vec = var.placebo,
                                            var.df = var.df))
sink()

